package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SpeciesUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.IntegrationChangeHistory;
import com.ruoyi.system.service.IIntegrationChangeHistoryService;
import com.ruoyi.system.service.ISpeciesUserService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SmsSpeciesReportMapper;
import com.ruoyi.system.domain.SmsSpeciesReport;
import com.ruoyi.system.service.ISmsSpeciesReportService;

import javax.annotation.Resource;

/**
 * 物种上报Service业务层处理
 *
 * @author DRIES
 * @date 2022-03-09
 */
@Service
public class SmsSpeciesReportServiceImpl implements ISmsSpeciesReportService
{
    @Autowired
    private SmsSpeciesReportMapper smsSpeciesReportMapper;

    @Resource
    private IIntegrationChangeHistoryService integrationChangeHistoryService;

    @Autowired
    private ISpeciesUserService speciesUserService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询物种上报
     *
     * @param id 物种上报主键
     * @return 物种上报
     */
    @Override
    public SmsSpeciesReport selectSmsSpeciesReportById(Long id)
    {
        return smsSpeciesReportMapper.selectSmsSpeciesReportById(id);
    }

    /**
     * 查询物种上报列表
     *
     * @param smsSpeciesReport 物种上报
     * @return 物种上报
     */
    @Override
    public List<SmsSpeciesReport> selectSmsSpeciesReportList(SmsSpeciesReport smsSpeciesReport)
    {
        return smsSpeciesReportMapper.selectSmsSpeciesReportList(smsSpeciesReport);
    }

    /**
     * 新增物种上报
     *
     * @param smsSpeciesReport 物种上报
     * @return 结果
     */
    @Override
    public int insertSmsSpeciesReport(SmsSpeciesReport smsSpeciesReport)
    {
        smsSpeciesReport.setCreateTime(DateUtils.getNowDate());
        return smsSpeciesReportMapper.insertSmsSpeciesReport(smsSpeciesReport);
    }

    /**
     * 修改物种上报
     *
     * @param smsSpeciesReport 物种上报
     * @return 结果
     */
    @Override
    public int updateSmsSpeciesReport(SmsSpeciesReport smsSpeciesReport)
    {
        smsSpeciesReport.setUpdateTime(DateUtils.getNowDate());
        if (smsSpeciesReport.getStatus() == 1) {
            // 查询用户信息
            SpeciesUser userInfo = speciesUserService.selectSpeciesUserByOpenId(smsSpeciesReport.getUserId());
            if (ObjectUtil.isNull(userInfo)) {
                return 0;
            }
            String integral = configService.selectConfigByKey("sms.report.send.integral");
            if (StrUtil.isNotBlank(integral)) {
                // 赠送积分
                userInfo.setIntegral(userInfo.getIntegral() + Long.parseLong(integral));
                speciesUserService.updateSpeciesUser(userInfo);
                // 插入积分记录
                IntegrationChangeHistory integrationChangeHistory = new IntegrationChangeHistory();
                integrationChangeHistory.setMemberId(userInfo.getId());
                integrationChangeHistory.setChangeType("0");
                integrationChangeHistory.setChangeCount(Long.valueOf(integral));
                integrationChangeHistory.setRemark("外来物种上报奖励");
                integrationChangeHistoryService.insertIntegrationChangeHistory(integrationChangeHistory);
            }

        }
        return smsSpeciesReportMapper.updateSmsSpeciesReport(smsSpeciesReport);
    }

    /**
     * 批量删除物种上报
     *
     * @param ids 需要删除的物种上报主键
     * @return 结果
     */
    @Override
    public int deleteSmsSpeciesReportByIds(Long[] ids)
    {
        return smsSpeciesReportMapper.deleteSmsSpeciesReportByIds(ids);
    }

    /**
     * 删除物种上报信息
     *
     * @param id 物种上报主键
     * @return 结果
     */
    @Override
    public int deleteSmsSpeciesReportById(Long id)
    {
        return smsSpeciesReportMapper.deleteSmsSpeciesReportById(id);
    }
}
